package com.example.jdbc.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

/**
 * jdbc配置类
 *
 * @PropertySource 能读取properties文件，比较方便
 * 是 spring context 的注解，
 * 优点：能读取任何文件
 */
@Configuration
@PropertySource("classpath:/jdbc.properties")   //只能解析properties文件 yml文件解析不了
public class JdbcConfiguration {

    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("jdbc.url")
    private String jdbcUrl;
    @Value("jdbc.userName")
    private String userName;
    @Value("jdbc.password")
    private String password;

    /**
     * 使用spring context 的@PropertySource 注解虽然可以读取properties 中的属性
     * 但是不能共享，每次使用前都要读取，造成资源浪费
     *
     * @return
     */
    //@Bean //注释@Bean  以防止注入失败
    public DataSource getDataSourceByAnnotation() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(this.jdbcUrl);
        druidDataSource.setUsername(this.userName);
        druidDataSource.setPassword(this.password);
        druidDataSource.setDriverClassName(this.driverClassName);
        return druidDataSource;
    }


    /**
     * 自己写的读取jdbc.properties文件 并将数据写入到DataSource中
     * 缺点：资源路径写死（比较致命）
     *
     * @return
     * @throws FileNotFoundException
     * @throws SQLException
     */
    //@Bean
    public DataSource getDataSource() throws FileNotFoundException {

        DruidDataSource druidDataSource = new DruidDataSource();

        Properties properties = new Properties();


        /*也获取不到资源

        InputStream resourceAsStream = this.getClass().getResourceAsStream("classpath:/jdbc.properties");

        System.out.println("resourceAsStream = " + resourceAsStream);*/
        /**
         * 文件路径不可以加classpath:/
         */
        FileInputStream in = new FileInputStream("/Volumes/OS/javaEE/springBoot/jdbc/src/main/resources/jdbc.properties");

        /**
         * 如果只是获取路径
         *
         */
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"jdbc.properties";

        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        properties.list(System.out);

        druidDataSource.setUrl(properties.getProperty("jdbc.url"));

        druidDataSource.setUsername(properties.getProperty("jdbc.userName"));

        druidDataSource.setPassword(properties.getProperty("jdbc.password"));

        Class<Driver> c = null;
        try {
            c = (Class<Driver>) Class.forName(properties.getProperty("jdbc.driverClassName"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Driver driver = null;
        try {
            driver = c.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        druidDataSource.setDriver(driver);

        /*
            这里是写死
        try {
            druidDataSource.setDriver(new com.mysql.cj.jdbc.Driver());
        } catch (SQLException e) {
            e.printStackTrace();
        }*/

        System.out.println("druidDataSource.getUrl() = " + druidDataSource.getUrl());

        System.out.println("druidDataSource.getDriverClassName() = " + druidDataSource.getDriverClassName());

        System.out.println("druidDataSource.getDriver() = " + druidDataSource.getDriver());

        System.out.println("druidDataSource.getUsername() = " + druidDataSource.getUsername());

        System.out.println("druidDataSource.getPassword() = " + druidDataSource.getPassword());


        return druidDataSource;
    }
}
